package lleida.festamajor.activities;

import lleida.festamajor.R;
import lleida.festamajor.controllers.EventController;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class AEvents extends FMActivity implements OnItemClickListener {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.events);
		//EventController.setActiveDate(0);
		
		//Omple la llista amb el EventAdapter
		final ListView elist = (ListView) findViewById(R.id.events_list);

		//Crea Spinner per les dates possibles
		Spinner spDates = (Spinner) findViewById(R.id.events_dies);
		ArrayAdapter<String> adapterDates = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, EventController.getDates()); //.createFromResource(this, R.array.dates, android.R.layout.simple_spinner_item);
		adapterDates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spDates.setAdapter(adapterDates);
		spDates.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				EventController.setActiveDate(position);
				elist.setAdapter(new EventAdapter(getBaseContext()));
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}});
		
		//Crea Spinner per les hores 
		Spinner spHores = (Spinner) findViewById(R.id.events_hores);
		ArrayAdapter<CharSequence> adapterHores = ArrayAdapter.createFromResource(this, R.array.hores, android.R.layout.simple_spinner_item);
		adapterHores.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spHores.setAdapter(adapterHores);
		spHores.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				String time = Integer.toString((position + 6)%23) + ":00";
				if (position < 4 || position > 18) time = "0" + time;
				int pos = EventController.getPositionByTime(time); 
				elist.setSelection(pos);
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}});
		
		int day = EventController.init();
		spDates.setSelection(day);
		elist.setAdapter(new EventAdapter(this));
		elist.setOnItemClickListener(this);
	}
	
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		EventController.setSelectedEvent(position);
		app.showEventDetail();
		//finish();
	}
	
	//Adapter pels elements de la llista
	private class EventAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		
		public EventAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return EventController.getNumEvents();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = mInflater.inflate(R.layout.event_list_item, null);
			
			TextView tName = (TextView) convertView.findViewById(R.id.events_item_name);
			TextView tPlace = (TextView) convertView.findViewById(R.id.events_item_place);
			TextView tTime = (TextView) convertView.findViewById(R.id.events_item_time);
			
			tName.setText(EventController.getEventName(position));
			tPlace.setText("@" + EventController.getEventPlace(position));
			tTime.setText(EventController.getEventTime(position));
			
			ImageView catIcon = (ImageView) convertView.findViewById(R.id.events_item_category);
			switch (EventController.getEventCategoryIcon(position)) {
			case 0:
				catIcon.setBackgroundResource(R.drawable.cultura);
				break;
			case 1:
				catIcon.setBackgroundResource(R.drawable.esport);
				break;
			case 2:
				catIcon.setBackgroundResource(R.drawable.musica);
				break;
			case 3:
				catIcon.setBackgroundResource(R.drawable.teatre);
				break;
			case 4:
				catIcon.setBackgroundResource(R.drawable.infantil);
				break;
			case 5:
				catIcon.setBackgroundResource(R.drawable.institucio);
				break;
			case 6:
				catIcon.setBackgroundResource(R.drawable.tecno);
				break;
			case 7:
				catIcon.setBackgroundResource(R.drawable.museu);
				break;
			}
			
			ImageView priceIcon = (ImageView) convertView.findViewById(R.id.events_price);
			if (!EventController.getEventFree(position)) priceIcon.setImageResource(R.drawable.euro);
			else priceIcon.setImageResource(0);
			
			return convertView;
		}
		
	}
}
